package com.chinasi.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.chinasi.model.ConsumerBasic;
import com.chinasi.model.DictData;
import com.chinasi.model.IndustryInfo;
import com.chinasi.model.IntegratorBasic;
import com.chinasi.model.ProductInfo;
import com.chinasi.model.ProjectInfo;
import com.chinasi.service.BidBasicService;
import com.chinasi.service.IndustryInfoService;
import com.chinasi.service.IntegratorBasicService;
import com.chinasi.service.ProductInfoService;
import com.chinasi.service.ProjectInfoService;
import com.chinasi.web.action.common.DictCache;
import com.chinasi.web.action.common.SystemConstants;
import com.chinasi.web.action.common.URLConstants;

/**
 * 解决方案
 * @author xzl
 *
 */
@Controller
@RequestMapping(value = URLConstants.PROJECT)
public class ProjectInfoAction {

	@Autowired
	private IndustryInfoService industryInfoService;
	
	@Autowired
	private ProjectInfoService projectService;
	
	@Autowired
	private ProductInfoService productService;
	
	@Autowired
	private BidBasicService wowBidBasicService;
	
	@Autowired
	private IntegratorBasicService integratorservice;
	
	/** 解决方案列表 **/
	@RequestMapping("list")
	public ModelAndView list(ProjectInfo searchBean){
		Map<String, Object> model = new HashMap<String, Object>();
		
//		//行业资讯
//		model.put("infoList", industryInfoService.findPageCondition(new IndustryInfo(),1));
		
		//解决方案
		model.put("projectList", projectService.findPageCondition(searchBean,1));
		
//		//创新产品
//		model.put("productList", productService.findPageCondition(new ProductInfo(),1));
		
		model.put("params", searchBean);
		
		model.put("menu", "FAZX");
		
		//两级行业分类
		HashMap<String, List<DictData>> classfyMap = new HashMap<String, List<DictData>>();
		HashMap<String, String> idNameMap = new HashMap<String, String>();
		List<DictData> list =  DictCache.getByCode(SystemConstants.DictCode.hangyefenlei.toString(), DictData.class);
		//两次遍历行业分类，组装行业两级分类数据
		for(DictData dictData : list){
			if(dictData.getParentoid() == null){
				ArrayList<DictData> items = new ArrayList<DictData>();
				classfyMap.put(dictData.getDictdataText(), items);
				idNameMap.put(dictData.getDictdataoid(),dictData.getDictdataText());
			}
		}
		for(DictData dictData : list){
			if(dictData.getParentoid() != null){
				List<DictData> items = classfyMap.get(idNameMap.get(dictData.getParentoid()));
				items.add(dictData);
			}
		}
		
		model.put("classfyMap", classfyMap);
		
		return new ModelAndView("jjfa/list",model);
	}
	
	/** 解决方案信息页 **/
	@RequestMapping("detail")
	public ModelAndView detail(String id){
		Map<String, Object> model = new HashMap<String, Object>();
		
		//解决方案信息
		ProjectInfo project = projectService.findById(id);
		model.put("project", project);
		
		//集成商
		IntegratorBasic integrator = integratorservice.findById(project.getOrgId());
		model.put("integrator", integrator);
		
		//相关资讯
		IndustryInfo industryInfoSearchBean = new IndustryInfo();
//		industryInfoSearchBean.setInfoTag(project.getProjTag());
		model.put("infoList", industryInfoService.findPageCondition(industryInfoSearchBean, 1));
		
		//可能感兴趣
		model.put("projectList", projectService.findPageCondition(new ProjectInfo(),1));
		
		return new ModelAndView("jjfa/info",model);
	}
	
	/** 异步加载分页数据 */
	@RequestMapping("loadajax")
	public ModelAndView loadajax(HttpServletRequest request, HttpServletResponse response,ProjectInfo searchBean, int curpage){
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("projectList", projectService.findPageCondition(searchBean,curpage));
		return new ModelAndView("fragment/fragment_consumer_more", model);
	}
}
